package com.xm.servlet;

import com.xm.biz.CartBiz;
import com.xm.biz.GoodsBiz;
import com.xm.biz.impl.CartBizImpl;
import com.xm.biz.impl.GoodsBizImpl;
import com.xm.po.Cart;
import com.xm.po.Goods;
import com.xm.po.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-19-9:42
 * @description 购物车数量修改处理器
 */
@WebServlet("/changeCount")
public class ChangeCountServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        CartBiz cartBiz = new CartBizImpl();
        GoodsBiz goodsBiz = new GoodsBizImpl();
        //获取修改类型
        String type = req.getParameter("type");
        //获取商品id
        Integer id = Integer.valueOf(req.getParameter("id"));
        HttpSession session = req.getSession();
        //取出用户信息
        Users user = (Users) session.getAttribute("user");
        //根据id查询商品信息
        Goods goods = goodsBiz.getGoodsByGoodsId(id);
        //根据商品id和用户id查询购物车信息
        Cart cartById = cartBiz.getCartById(id, user.getUserId());
        if (type.equals("plus")){
            //plus加数量
            if (cartById.getGoodsCnt()+1>goods.getGoodsStock()){
                //弹窗库存不足
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('商品库存不足!');location.href='shopcart.jsp';</script>");
                out.flush();
                out.close();
            }else {
                //修改数量
                cartById.setGoodsCnt(cartById.getGoodsCnt()+1);
            }
        }else if (type.equals("lose")){
            //lose减数量
            if (cartById.getGoodsCnt()-1<1){
                //弹窗提示不能再减了
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('商品数量不能为0!');location.href='shopcart.jsp';</script>");
                out.flush();
                out.close();
            }else {
                //修改数量
                cartById.setGoodsCnt(cartById.getGoodsCnt()-1);
            }
        }else if (type.equals("change")){
            //获取修改的数量
            Integer count = Integer.valueOf(req.getParameter("count"));
            //change输入数量
            if (count>goods.getGoodsStock()){
                //弹窗库存不足
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('商品库存不足!');location.href='shopcart.jsp';</script>");
                out.flush();
                out.close();
            }else {
                cartById.setGoodsCnt(count);
            }
        }
        //修改购物车指定商品数量
        boolean result = cartBiz.updateCart(cartById);
        if (result){
            List<Cart> cartList = cartBiz.getAll(user.getUserId());
            List<Cart> cartByIdChecked = cartBiz.getCartByIdChecked(user.getUserId());
            session.setAttribute("sum",cartByIdChecked);
            session.setAttribute("cartList",cartList);
            resp.sendRedirect("shopcart.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
